﻿Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Net.Mail
Imports System.Net
Imports System.Threading

Public Class Common
    ' Many of the common functions and procedure used throughout this application should be placed in this class.

    ' To get a setting stored within the Settings table within the database:
    ' GetSetting(settingName)
    ' settingName = The name of the setting stored in the database.

    ' To send an email:
    ' SendMail(recipient, subject, body)
    ' Refer to the XML documentation for this procedure.

    ''' <summary>
    ''' Set the database connection variables used throughout this class.
    ''' </summary>
    ''' <remarks>Placed here so they will not need to be typed out each time they are needed.</remarks>
    Private Shared connectionString As String = WebConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString()
    Private Shared connection As SqlConnection = New SqlConnection(connectionString)
    Private Shared command As New SqlCommand

    ''' <summary>
    ''' Gets the specified setting from the database.
    ''' </summary>
    ''' <param name="setting">The setting.</param>
    ''' <returns></returns>
    Public Shared Function GetSetting(ByVal setting As String) As String
        Dim value As String = Nothing
        Try
            command = New SqlCommand("sp_public_getSetting", connection)
            command.CommandType = CommandType.StoredProcedure
            command.Parameters.Add("@Setting", SqlDbType.NVarChar, 50)
            command.Parameters("@Setting").Value = setting
            command.Parameters.Add("@Value", SqlDbType.NVarChar, -1)
            command.Parameters("@Value").Direction = ParameterDirection.Output
            connection.Open()
            command.ExecuteNonQuery()
            value = CStr(command.Parameters("@Value").Value)
        Catch ex As Exception

        Finally
            connection.Close()
        End Try

        Return value
    End Function

    ''' <summary>
    ''' Sends an e-mail to the person who made the request.
    ''' </summary>
    ''' <param name="recipient">The member's e-mail address.</param>
    ''' <param name="subject">The subject of this email.</param>
    ''' <param name="body">The body of this email.</param>
    Public Shared Sub SendMail(ByVal recipient As String, ByVal subject As String, ByVal body As String)
        ' Instantiate a new instance of MailMessage.
        Dim mailMsg As MailMessage = New MailMessage()

        ' Begin to put the activation e-mail together.
        mailMsg.From = New MailAddress(CStr(GetSetting("EmailFrom")))
        mailMsg.To.Add(recipient)
        mailMsg.Subject = subject
        mailMsg.Body = body
        mailMsg.IsBodyHtml = True
        mailMsg.Priority = MailPriority.Normal

        ' Instantiate a new instance of SmtpClient.
        Dim mSmtpClient As New SmtpClient(CStr(GetSetting("EmailServer")), GetSetting("EmailPort"))

        ' If SMTP authentication is required pass the credentials.
        If CBool(GetSetting("EmailAuthenticate")) Then
            mSmtpClient.Credentials = New NetworkCredential(CStr(GetSetting("EmailLogin")), CStr(GetSetting("EmailPassword")))
        End If

        ' Send the activation e-mail to the registrant.
        Try
            mSmtpClient.Send(mailMsg)
        Catch ex As SmtpFailedRecipientException
            Dim statusCode As SmtpStatusCode = ex.StatusCode
            If statusCode = SmtpStatusCode.MailboxBusy OrElse statusCode = SmtpStatusCode.MailboxUnavailable OrElse statusCode = SmtpStatusCode.TransactionFailed Then
                ' If the e-mail could not be sent for one of the above reasons wait 5 seconds and try again.
                Thread.Sleep(5000)
                mSmtpClient.Send(mailMsg)
            Else
                Throw
            End If
        Finally
            ' Dispose of the mail message.
            mailMsg.Dispose()
        End Try
    End Sub
End Class
